package com.xunchi.haichuan.common.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.xunchi.haichuan.user.action.LoginAction;
import com.xunchi.haichuan.user.model.User;

/**
 * 作者：kongxb<br>
 * 模块名称：公共模块<br>
 * 功能描述：检查用户是否登录拦截器<br>
 * 维护记录：2009.11.21, kongxb 创建该类
 */
public class CheckLoginInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 3720443199567362371L;

	public static final String USER_SESSION_KEY = "currentUser";

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		System.out.println("Begin check login interceptor...");

		// 对LoginAction不做该项拦截
		Object action = actionInvocation.getAction();
		if (action instanceof LoginAction) {
			System.out.println("Exit check login, because this is login action.");
			return actionInvocation.invoke();
		}
		// 验证 session
		Map session = actionInvocation.getInvocationContext().getSession();
		User currentUser = (User) session.get(USER_SESSION_KEY);

		if (currentUser != null) {
			// 存在的情况下进行后续操作。
			System.out.println(currentUser.getName() + " already login!");
			return actionInvocation.invoke();
		} else {
			// 否则终止后续操作，返回LOGIN
			System.out.println("No login, forward login page!");
			return Action.LOGIN;

		}

	}
}
